package com.sky.service.user.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.mapper.common.DishMapper;
import com.sky.mapper.common.SetmealMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.Setmeal;
import com.sky.pojo.ShoppingCart;
import com.sky.service.common.SetmealService;
import com.sky.service.user.ShoppingCartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private static final Logger log = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO 我每写一行请帮我注释详细一点
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车:{}",shoppingCartDTO);
        ShoppingCart shoppingCart = new ShoppingCart();//购物车对象
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);//拷贝属性
        //只能查询自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("购物车数据：{}" + BaseContext.getCurrentId());
        //查询当前菜品或套餐是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            //购物车中存在，数量+1
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {
            //如果不存在，添加到购物车，默认为一份

            //判断当前添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //添加的是菜品
                Dish dish = dishMapper.getById(dishId);//根据id查询菜品数据
                shoppingCart.setName(dish.getName());//设置菜品名称
                shoppingCart.setImage(dish.getImage());//设置菜品图片
                shoppingCart.setAmount(dish.getPrice());//设置菜品价格
            }else {
                //添加的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());//根据id查询套餐数据
                shoppingCart.setName(setmeal.getName());//设置套餐名称
                shoppingCart.setImage(setmeal.getImage());//设置套餐图片
                shoppingCart.setAmount(setmeal.getPrice());//设置套餐价格
            }//
            shoppingCart.setNumber(1);//设置份数为1
            shoppingCart.setCreateTime(LocalDateTime.now());//设置创建时间为当前时间
            shoppingCartMapper.insert(shoppingCart);
        }

    }


    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showshoppingCart() {
        List<ShoppingCart> shoppingCartList =
      shoppingCartMapper.selectByUserId(BaseContext.getCurrentId());
        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中的某一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        log.info("减少购物车项目:{}", shoppingCartDTO);

        ShoppingCart shoppingCart = new ShoppingCart(); // 创建购物车对象
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart); // 拷贝属性
        shoppingCart.setUserId(BaseContext.getCurrentId()); // 设置用户ID，确保只能查询自己的购物车数据

        // 查询当前菜品或套餐是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        if (shoppingCartList != null && !shoppingCartList.isEmpty()) {
            // 假设我们只处理第一个匹配项
            ShoppingCart existingItem = shoppingCartList.get(0);

            if (existingItem.getNumber() > 1) {
                // 如果数量大于1，数量减1
                existingItem.setNumber(existingItem.getNumber() - 1);
                shoppingCartMapper.updateNumberById(existingItem); // 更新数量
            } else {
                // 如果数量等于1，删除该记录
                shoppingCartMapper.deleteById(existingItem.getId());
            }
        } else {
            // 如果购物车中不存在该项，可以抛出异常或进行其他处理
            throw new RuntimeException("购物车中不存在要减少的项目");
        }
    }
    }

